<?php

/**
 * @Author: Mr.Mao
 * @Date:   2021-05-31 09:46:50
 * @Last Modified by:   Mr.Mao
 * @Last Modified time: 2021-06-02 17:07:05
 */
/**
 * 腾讯云存储
 */
require('aliyun-oss-php-sdk/autoload.php');
use app\mainadmin\model\StorageModel;
use OSS\OssClient;
use OSS\Core\OssException;
class storage
{
	
	public $config;
	public $obj;
	public $bucket;
	public $region;
	// 初始化
	public function __construct(){
		$where = [];
		$where[] = ['type','=','oss'];
		$info = (new StorageModel)->getInfo($where);
		$this->config = $info['config'];
		try {
		    $this->obj = new OssClient($this->config['accessKeyId'], $this->config['accessKeySecret'],$this->config['region']);
		} catch (OssException $e) {
		    printf(__FUNCTION__ . ": FAILED\n");
		    printf($e->getMessage() . "\n");
		    die ;
		}
		$this->bucket = $this->config['bucket'];
	}
	/**
	 * [createBucket 创建存储空间]
	 * @return [type] [空间名称]
	 */
	public function createBucket($bucket)
	{
		try {
		    $res = $this->obj->createBucket($bucket);
		} catch (OssException $e) {
			$data['code'] = 0;
			$data['msg'] = $e->getMessage();
		    return $data;
		}
	    $data['code'] = 1;
	    return $data;
	}
	/**
	 * [getBucketList 获取存储空间列表]
	 */
	public function getBucketList()
	{
		try {
		   	$bucketListInfo = $this->obj->listBuckets();
		} catch (OssException $e) {
		    $data['code'] = 0;
			$data['msg'] = $e->getMessage();
		    return $data;
		}
	    $bucketList = $bucketListInfo->getBucketList();
	    $list = [];
	    foreach($bucketList as $bucket) {
		    $list[] = $bucket;
		} 
	    $data['code'] = 1;
	    $data['list'] = $list;
	    return $data;
	}
	/**
	 * [doesBucketExist 判断空间是否存在]
	 * @param  string $bucket [空间名称]
	 */
	public function doesBucketExist($bucket)
	{
		try {
		    $res = $this->obj->doesBucketExist($bucket);
		} catch (OssException $e) {
		    $data['code'] = 0;
			$data['msg'] = $e->getMessage();
		    return $data;
		}
		$data['code'] = 1;
		if ($res === true) {
		    $data['msg'] = 'ok';
		    
		} else {
			$data['msg'] = 'fall';
		}
		return $data;
	    
	}
	/**
	 * [getBucketMeta 获取存储空间元信息]
	 * @param  string $bucket [空间名称]
	 */
	public function getBucketMeta($bucket)
	{
		try {
		    $res = $this->obj->getBucketMeta($bucket);
		} catch (OssException $e) {
		    $data['code'] = 0;
			$data['msg'] = $e->getMessage();
		    return $data;
		}
		$data['code'] = 1;
		$data['metas'] = $res;
		return $data;
	}

	/**
	 * [getBucketInfo 获取存储空间信息]
	 * @param  string $bucket [空间名称]
	 */
	public function getBucketInfo($bucket)
	{
		try {
		    $res = $this->obj->getBucketInfo($bucket);
		} catch (OssException $e) {
		    $data['code'] = 0;
			$data['msg'] = $e->getMessage();
		    return $data;
		}
	    $data['code'] = 1;
		$data['Name'] = $res->getName();
		$data['Location'] = $res->getLocation();
		$data['CreateDate'] = $res->getCreateDate();
		$data['StorageClass'] = $res->getStorageClass();
		$data['ExtranetEndpoint'] = $res->getExtranetEndpoint();
		$data['IntranetEndpoint'] = $res->getIntranetEndpoint();
		return $data;
	}
	/**
	 * [deleteBucket 删除存储空间]
	 * @param  string $bucket [空间名称]
	 */
	public function deleteBucket($bucket)
	{
		try {
		    $res = $this->obj->deleteBucket($bucket);
		} catch (OssException $e) {
		    $data['code'] = 0;
			$data['msg'] = $e->getMessage();
		    return $data;
		}
		$data['code'] = 1;
	    return $data;
	}
	/**
	 * [upload 上传文件]
	 * @param  string $object   [文件名称]
	 * @param  array  $file   	[上传的文件]
	 */
	public function upload($object = '',$file = [])
	{
		try {
		    $res = $this->obj->uploadFile($this->bucket,$object,$file['tmp_name']);
		} catch (OssException $e) {
		   	$data['code'] = 0;
			$data['msg'] = $e->getMessage();
		    return $data;
		}
		$data['code'] = 1;
		$data['url'] = $res['oss-request-url'];
	    return $data;
	}
	/**
	 * [getObject 下载文件]
	 * @param  string $object [文件名]
	 */
	public function getObject($object='')
	{
		$options = array(
	        OssClient::OSS_FILE_DOWNLOAD => 'd:\Users\Administrator\Desktop\test\test.jpg'
	    );
		try {
		    $res = $this->obj->getObject($this->bucket,$object,$options);
		} catch (OssException $e) {
		    printf(__FUNCTION__ . ": FAILED\n");
		    printf($e->getMessage() . "\n");
		    return;
		}
		print(__FUNCTION__ . ": OK, please check localfile: 'upload-test-object-name.jpg'" . "\n");
	}
	/**
	 * [doesObjectExist 判断文件是否存在]
	 * @param  string $object [文件名]
	 */
	public function doesObjectExist($object='')
	{
		try{
		    $exist = $this->obj->doesObjectExist($this->bucket, $object);
		} catch(OssException $e) {
		    $data['code'] = 0;
			$data['msg'] = $e->getMessage();
		    return $data;
		}
		if ($exist == 1) {
			$data['msg'] = 'ok';
		}else{
			$data['msg'] = 'fall';
		}
		$data['code'] = 1;
		
	    return $data;
	}
	/**
	 * [getSimplifiedObjectMeta 获取文件源信息]
	 * @param  string $object [description]
	 * @return [type]         [description]
	 */
	public function getSimplifiedObjectMeta($object='')
	{
		try{
		    $objectMeta  = $this->obj->getSimplifiedObjectMeta($this->bucket, $object);
		} catch(OssException $e) {
		    $data['code'] = 0;
			$data['msg'] = $e->getMessage();
		    return $data;
		}
		$data['code'] = 1;
		$data['info'] = $objectMeta;
	    return $data;
	}

	/**
	 * [listObjects 列举所有文件]
	 * @param  array  $options [请求参数]
	 */
	public function listObjects($options=[])
	{
		$nextMarker = '';
		while (true) {
		    try {
		        $options = array(
		            'delimiter' => '',
		            'marker' => $nextMarker,
		        );
		        $listObjectInfo =  $this->obj->listObjects($this->bucket, $options);
		    } catch (OssException $e) {
		        printf(__FUNCTION__ . ": FAILED\n");
		        printf($e->getMessage() . "\n");
		        return;
		    }
		    // 得到nextMarker，从上一次listObjects读到的最后一个文件的下一个文件开始继续获取文件列表。
		    $nextMarker = $listObjectInfo->getNextMarker();
		    $listObject = $listObjectInfo->getObjectList();
		    $listPrefix = $listObjectInfo->getPrefixList();

		    if (!empty($listObject)) {
		        print("objectList:\n");
		        foreach ($listObject as $objectInfo) {
		            print($objectInfo->getKey() . "\n");
		        }
		    }
		    if (!empty($listPrefix)) {
		        print("prefixList: \n");
		        foreach ($listPrefix as $prefixInfo) {
		            print($prefixInfo->getPrefix() . "\n");
		        }
		    }
		    if ($listObjectInfo->getIsTruncated() !== "true") {
		       break;
		    }
		}
	}
	/**
	 * [copyObject 拷贝文件]
	 * @param  [type] $from_object [源文件名]
	 * @param  [type] $to_object   [目标文件名]
	 * @param  [type] $to_bucket   [目标桶]
	 */
	public function copyObject($from_object,$to_object, $to_bucket = '')
	{
		if (empty($to_bucket)) {
			$to_bucket = $this->bucket;
		}
		try{

		    $this->obj->copyObject($this->bucket, $from_object, $to_bucket, $to_object);
		} catch(OssException $e) {
		   	$data['code'] = 0;
			$data['msg'] = $e->getMessage();
		    return $data;
		}
		return ['code' => 1];
	}
	/**
	 * [deleteObject 删除单个文件]
	 * @param  string $object [文件名称]
	 */
	public function deleteObject($object='')
    {
        try{
		    $this->obj->deleteObject($this->bucket, $object);
		} catch(OssException $e) {
		    $data['code'] = 0;
			$data['msg'] = $e->getMessage();
		    return $data;
		}
		return ['code' => 1];
    }

    /**
     * [deleteObjects 删除多个文件]
     * @param  array  $objects [删除对象]
     */
	public function deleteObjects($objects=[])
    {
        try{
		    $this->obj->deleteObjects($this->bucket, $objects);
		} catch(OssException $e) {
		    $data['code'] = 0;
			$data['msg'] = $e->getMessage();
		    return $data;
		}
		return ['code' => 1];
    }
}